#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using System.Configuration;
using Agresso.Interface.Layer.Service;
using Agresso.Interface.Layer.Domain;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserControlScheduleTask : UserControl
    {
        string m_winserviceFile = ConfigurationManager.AppSettings["WindowsServiceFile"];
        ConfigurationSettingService m_service = new ConfigurationSettingService();
        ConfigurationSetting m_configurationSettingTCRElet;
        //ConfigurationSetting m_configurationSettingVoyageCargoRelet;
        ConfigurationSetting m_configurationSettingProforma;
        ConfigurationSetting m_configurationSettingFreightHire;
        ConfigurationSetting m_configurationSettingWinSDSPO;
        ConfigurationSetting m_configurationSettingVoyageMaster;
        ConfigurationSetting m_configurationSettingStatement;
        ConfigurationSetting m_configurationSettingBunkerPO;
        ConfigurationSetting m_configurationSettingBunkerGRN;
        ConfigurationSetting m_configurationSettingBunkerInventory;
        ConfigurationSetting m_configurationSettingEmailAlert;
        ConfigurationSetting m_configurationSettingCrewPayroll;

        public UserControlScheduleTask(string name, User currentUser, MainForm mainForm)
        {
            this.Name = name;
            InitializeComponent();
            this.labelTitle.Text = name;
            initServiceButton();
            initToolBarButtonClick();
            loadConfigurationSettings();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void initToolBarButtonClick()
        {
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonExecute.Click += new EventHandler(toolBarButtonExecute_Click);
        }

        void toolBarButtonExecute_Click(object sender, EventArgs e)
        {
            if(listViewSchedule.SelectedItem == null)return;
            if(listViewSchedule.SelectedItem.Tag == null)return;
            ConfigurationSetting conf = (ConfigurationSetting)listViewSchedule.SelectedItem.Tag;
            MessageBox.Show(string.Format("Are you sure to execute selected ?", conf.setting_name), "Run Selected Interface", MessageBoxButtons.YesNo, new EventHandler(Execute));
        }

        void Execute(object p, EventArgs a)
        {
            if (listViewSchedule.SelectedItem == null) return;
            if (listViewSchedule.SelectedItem.Tag == null) return;
            ConfigurationSetting conf = (ConfigurationSetting)listViewSchedule.SelectedItem.Tag;
            MessageBoxWindow msg = (MessageBoxWindow)p;
            if (msg.DialogResult == DialogResult.Yes)
            {
                ExecuteInterface.Execute(conf);
            }
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            initServiceButton();
            loadConfigurationSettings();            
        }

        private void loadConfigurationSettings()
        {
            try
            {
                m_configurationSettingTCRElet = m_service.GetConfigurationSetting("TCRelet");
                //m_configurationSettingVoyageCargoRelet = m_service.GetConfigurationSetting("VoyageCargoRelet");
                m_configurationSettingFreightHire = m_service.GetConfigurationSetting("FreightHireInvoice");
                m_configurationSettingWinSDSPO = m_service.GetConfigurationSetting("WinSDSPO");
                m_configurationSettingVoyageMaster = m_service.GetConfigurationSetting("VoyageMaster");
                m_configurationSettingStatement = m_service.GetConfigurationSetting("Statement");
                m_configurationSettingBunkerPO = m_service.GetConfigurationSetting("BunkerPO");
                m_configurationSettingBunkerGRN = m_service.GetConfigurationSetting("BunkerGRN");
                m_configurationSettingBunkerInventory = m_service.GetConfigurationSetting("BunkerInventory");
                m_configurationSettingEmailAlert = m_service.GetConfigurationSetting("EmailAlert");
                m_configurationSettingProforma = m_service.GetConfigurationSetting("Proforma");
                m_configurationSettingCrewPayroll = m_service.GetConfigurationSetting("CrewPayroll");
                listViewSchedule.Items.Clear();
                addConfigurationToList(m_configurationSettingTCRElet);
                //addConfigurationToList(m_configurationSettingVoyageCargoRelet);
                addConfigurationToList(m_configurationSettingProforma); 
                addConfigurationToList(m_configurationSettingFreightHire);
                addConfigurationToList(m_configurationSettingWinSDSPO);
                addConfigurationToList(m_configurationSettingVoyageMaster);
                addConfigurationToList(m_configurationSettingStatement);
                addConfigurationToList(m_configurationSettingBunkerPO);
                addConfigurationToList(m_configurationSettingBunkerGRN);
                addConfigurationToList(m_configurationSettingBunkerInventory);
                addConfigurationToList(m_configurationSettingEmailAlert);
                addConfigurationToList(m_configurationSettingCrewPayroll);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, x.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void addConfigurationToList(ConfigurationSetting conf)
        {
            ListViewItem item = listViewSchedule.Items.Add(conf.setting_name);
            item.SubItems.Add(conf.val_1);
            string[] time = conf.val_2.Split(':');
            item.SubItems.Add(time[0].PadLeft(2, '0') + ":" + time[1].PadLeft(2, '0'));
            item.SubItems.Add(Convert.ToBoolean(conf.val_11) ? "ENABLE" : "DISABLE");
            item.SubItems.Add(conf.val_10);
            item.Tag = conf;
        }

        private void initServiceButton()
        {
            try
            {
                bool installed = ServiceInstaller.ServiceIsInstalled("AgressoInterfaceLayerWinService.Production");
                buttonStart.Enabled = installed;
                if (installed)
                {
                    buttonInstallService.Text = "Uninstall Service";
                    labelServiceStatus.Text = ServiceInstaller.GetServiceStatus("AgressoInterfaceLayerWinService.Production").ToString();
                    if (ServiceInstaller.GetServiceStatus("AgressoInterfaceLayerWinService.Production") == ServiceState.Running)
                    {
                        buttonStart.Text = "Stop";
                    }
                    else
                    {
                        buttonStart.Text = "Start";
                    }
                }
                else 
                {
                    labelServiceStatus.Text = "Service Not Installed";
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, x.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            labelServiceStatus.Visible = !labelServiceStatus.Visible;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (buttonStart.Text == "Start")
                {
                    ServiceInstaller.StartService("AgressoInterfaceLayerWinService.Production");
                    buttonStart.Text = "Stop";
                }
                else
                {
                    ServiceInstaller.StopService("AgressoInterfaceLayerWinService.Production");
                    buttonStart.Text = "Start";
                }
                labelServiceStatus.Text = ServiceInstaller.GetServiceStatus("AgressoInterfaceLayerWinService.Production").ToString();

            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, x.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonInstallService_Click(object sender, EventArgs e)
        {
            try
            {
                if (buttonInstallService.Text == "Install Service")
                {
                    ServiceInstaller.InstallAndStart("AgressoInterfaceLayerWinService.Production", "AgressoInterfaceLayerWinService.Production", m_winserviceFile);
                    buttonInstallService.Text = "Uninstall Service";
                    buttonStart.Text = "Stop";
                    buttonStart.Enabled = true;
                    labelServiceStatus.Text = ServiceInstaller.GetServiceStatus("AgressoInterfaceLayerWinService.Production").ToString();
                }
                else
                {
                    ServiceInstaller.Uninstall("AgressoInterfaceLayerWinService.Production");
                    buttonInstallService.Text = "Install Service";
                    buttonStart.Enabled = false;
                    labelServiceStatus.Text = "Service Not Installed";
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, x.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonAdvanced_Click(object sender, EventArgs e)
        {
            AdvancedScheduleTaskForm asf = new AdvancedScheduleTaskForm(m_configurationSettingTCRElet);
            asf.ShowDialog();
        }

        private void listViewSchedule_DoubleClick(object sender, EventArgs e)
        {
            if(listViewSchedule.SelectedItem == null)return;
            if(listViewSchedule.SelectedItem.Tag == null)return;
            ConfigurationSetting conf = (ConfigurationSetting)listViewSchedule.SelectedItem.Tag;
            ScheduleTaskForm sch = new ScheduleTaskForm(conf,this);
            sch.ShowDialog();
        }

        internal void UpdateConfiguration(ConfigurationSetting conf)
        {
            if (listViewSchedule.SelectedItem == null) return;
            if (listViewSchedule.SelectedItem.Tag == null) return;
            listViewSchedule.SelectedItem.Text = conf.setting_name;
            listViewSchedule.SelectedItem.SubItems[0].Text = conf.setting_name;
            listViewSchedule.SelectedItem.SubItems[1].Text = conf.val_1;
            string[] time = conf.val_2.Split(':');
            listViewSchedule.SelectedItem.SubItems[2].Text = time[0].PadLeft(2, '0') + ":" + time[1].PadLeft(2, '0');
            listViewSchedule.SelectedItem.SubItems[3].Text = Convert.ToBoolean(conf.val_11) ? "ENABLE" : "DISABLE";
            listViewSchedule.SelectedItem.SubItems[4].Text = conf.val_10;
            listViewSchedule.SelectedItem.Tag = conf;
        }
    }
}